package cn.school.common.rabbitMQ;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.UUID;

/**
 * RabbitMQ消息发送
 */
@Component
@Slf4j
public class RabbitSenderService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 这里就是确认消息的回调监听接口，用于确认消息是否被broker所收到
     */
    final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        /**
         *    @param ack broker 是否落盘成功
         *    @param cause 失败的一些异常信息
         */
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            System.err.println("消息ACK结果:" + ack + ", correlationData: " + correlationData.getId());
        }
    };

    /**
     *
     * @param message 具体的消息内容
     * @param properties 额外的附加属性
     * @param exchange mq交换机
     * @param routingKey mq路由
     */
    public void sendMqMessage(Object message,
                              Map<String, Object> properties,
                              String exchange,
                              String routingKey){
        log.info("message Data : {}", JSON.toJSONString(message));
        MessageHeaders mhs = new MessageHeaders(properties);
        Message<?> msg = MessageBuilder.createMessage(message, mhs);

        rabbitTemplate.setConfirmCallback(confirmCallback);

        // 	指定业务唯一的iD
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        MessagePostProcessor mpp = new MessagePostProcessor() {

            @Override
            public org.springframework.amqp.core.Message postProcessMessage(org.springframework.amqp.core.Message message)
                    throws AmqpException {
                System.err.println("---> post to do: " + message.toString());
                return message;
            }
        };

        rabbitTemplate.convertAndSend(exchange, routingKey, msg, mpp, correlationData);
    }

    /**
     * 对外发送消息的方法
     *
     * @param message    具体的消息内容
     * @param properties 额外的附加属性
     * @throws Exception
     */
    public void sendMqMessage(Object message, Map<String, Object> properties) {
        log.info("message Data : {}", JSON.toJSONString(message));
        MessageHeaders mhs = new MessageHeaders(properties);
        Message<?> msg = MessageBuilder.createMessage(message, mhs);

        rabbitTemplate.setConfirmCallback(confirmCallback);

        // 	指定业务唯一的iD
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        MessagePostProcessor mpp = new MessagePostProcessor() {

            @Override
            public org.springframework.amqp.core.Message postProcessMessage(org.springframework.amqp.core.Message message)
                    throws AmqpException {
                System.err.println("---> post to do: " + message.toString());
                return message;
            }
        };

        rabbitTemplate.convertAndSend("iot-device-prod", "rabbit.leaveinfo", msg, mpp, correlationData);
//        rabbitTemplate.convertAndSend("test-exchanges", "test-routing", msg, mpp, correlationData);

    }

    public void sendStudentMessage(Object message, Map<String, Object> properties) {
        log.info("message Data : {}", JSON.toJSONString(message));
        MessageHeaders mhs = new MessageHeaders(properties);
        Message<?> msg = MessageBuilder.createMessage(message, mhs);

        rabbitTemplate.setConfirmCallback(confirmCallback);

        // 	指定业务唯一的iD
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        MessagePostProcessor mpp = new MessagePostProcessor() {

            @Override
            public org.springframework.amqp.core.Message postProcessMessage(org.springframework.amqp.core.Message message)
                    throws AmqpException {
                System.err.println("---> post to do: " + message.toString());
                return message;
            }
        };
        rabbitTemplate.convertAndSend("test-exchanges", "test-routing", msg, mpp, correlationData);

    }
}
